<?php
namespace app\spider\service\content;
use QL\QueryList;
use QL\Ext\CurlMulti;
use QL\Ext\AbsoluteUrl;

class ContentService extends AbstractBaseService
{
    /**
     *
     * @param QueryList $ql
     * @param CurlMulti $curl
     * @param $r
     * @param array $args
     * @return mixed|void
     */
    public function process(QueryList $ql, CurlMulti $curl, $r, $args = []){
        echo $r['info']['url'],"\n";
        preg_match('/^(https?:\/\/)?[\d\w]{0,}\.?[\d\w]+\.\w+\/?/i', $r['info']['url'],$baseUrl);

        $baseUrl = trim($baseUrl[0],'/');
        $site_info = $this->params[$baseUrl];

        $ql->html($r['body']);
        $charset = strtoupper($site_info['charset']);
        if($charset != 'UTF-8'){
            $ql->encoding('UTF-8', $charset);
        }

        $content_reg = $site_info['content_reg'];
        $rules['source'] = $content_reg['link'];
        if (isset($content_reg['link_text'])) {
            $rules['source_txt'] = $content_reg['link_text'];
        } else {
            $rules['source_txt'] = [$content_reg['link'][0], 'text'];
        }

        $sources = $ql->rules($rules)->query()->getData()->all();
        foreach($sources as $key=>$value){
            if($source = $this->checkSourceLink($value['source'], $baseUrl, $r['info']['url'])) {
                $sources[$key] = $source;
                $sources[$key]['title'] = isset($value['source_txt']) ? $value['source_txt'] : '';
            }else{
                unset($sources[$key]);
            }
        }

        $content = $ql->rules([
            'content' => [$content_reg['content'][0], 'text', 'p br']
        ])->query()->getData()->all();
        $content = empty($content) ? '' : $content[0]['content'];

        $this->saveData($sources, $r['info']['url'], $content, $site_info['id'], $args['title']);
    }
}
